const mask = document.querySelector('.mask')
const counter = document.querySelector('.counter span')

// 游戏类总领全文
class Game {
    constructor() {
        this.sky = new Sky()
        this.land = new Land()
        this.bird = new Bird()
        this.pipeProducer = new PipePairProducer(-100)
        this.timer = null 
        this.tick = 16
        this.gameOver = false
        this.goal = 0
        this.gap = 10
    }

    start() {
        if(this.timer) {
            return
        }
        if(this.gameOver) {
            window.location.reload()
        }
        this.pipeProducer.startProduce()
        this.bird.startWave()
        this.timer = setInterval(() => {
            const duration = this.tick / 1000
            this.sky.move(duration)
            this.land.move(duration)
            this.bird.move(duration)
            this.pipeProducer.pairs.forEach(pair => {
                // console.log(pair);
                pair.move(duration)
            })
            if(this.isGameOver()) {
                this.stop()
                this.gameOver = true
                mask.style.display = 'block'
            }
        }, this.tick)
    }

    isHit(rec1, rec2) {
        const centerX1 = rec1.left + rec1.width / 2
        const centerY1 = rec1.top + rec1.height / 2
        const centerX2 = rec2.left + rec2.width / 2
        const centerY2 = rec2.top + rec2.height / 2
        const disX = Math.abs(centerX1 - centerX2)
        const disY = Math.abs(centerY1 - centerY2)
        // console.log(disX, disY);
        // console.log((rec1.width + rec2.width) / 2, (rec1.height + rec2.height) / 2);
        if(disX < (rec1.width + rec2.width) / 2 && 
           disY < (rec1.height + rec2.height) / 2) {
           return true
        }
        return false
    }

    isGameOver() {
        // 和大地的碰撞检测
        if(this.bird.top === this.bird.birdMaxTop) {
            return true
        }
        for(let i = 0; i < this.pipeProducer.pairs.length; i++) {
            const pair = this.pipeProducer.pairs[i]
            if(this.isHit(this.bird, pair.upPipe) || this.isHit(this.bird, pair.downPipe)) {
                return true
            }
            this.count(pair.upPipe)
        }
        return false
    }

    count(pipe) {
        // 如果 pipePair.left + pipePair.width < bird.left 则加分
        if(pipe.left + pipe.width < this.bird.left) {
            if(!pipe.checked) {
                this.goal += this.gap
                counter.innerText = this.goal
                pipe.checked = true
            }
        }
    }

    stop() {
        clearInterval(this.timer)
        this.timer = null
        this.bird.stopWave()
        this.pipeProducer.stopProduce()
    }

    regEvent() {
        window.onkeydown = (e) => {
            if(e.key === 'Enter') {
                if(this.timer) {
                    this.stop()
                } else {
                    this.start()
                    mask.style.display = 'none'
                    this.goal = 0
                }
            } else if(e.key === ' ') {
                this.bird.jump()
            }
        }
    }
}

var g = new Game()
g.regEvent()